package com.zcxy.util;

import org.springframework.web.context.request.*;
import org.springframework.web.servlet.HandlerMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

/**
 * 获取 HttpServletRequest
 *
 * @author xl
 * @date 2019-11-24
 */
public class RequestHolder {
    public static String WORKSPACE_ID = "workspaceId";

    public static HttpServletRequest getHttpServletRequest() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }

    public static String getWorkSapceId() {
        return RequestHolder.getPathVariableBykey(RequestHolder.WORKSPACE_ID);
    }

    public static String getPathVariableBykey(String key) {
        HttpServletRequest httpServletRequest = RequestHolder.getHttpServletRequest();
        NativeWebRequest webRequest = new ServletWebRequest(httpServletRequest);
        Map<String, String> map = (Map<String, String>) webRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
        return map.get(key);

    }
    //获取相关信息，token


}
